package owl2vowl.converter;

import java.io.InputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;

public class InputStreamConverter extends AbstractConverter {
	protected InputStream mainOntology;
	protected Collection<InputStream> depdencyOntologies;
	private static final Logger logger = LogManager.getLogger(InputStreamConverter.class);

	public InputStreamConverter(InputStream ontology) {
		this(ontology, Collections.emptyList());
	}

	public InputStreamConverter(InputStream ontology, Collection<InputStream> necessaryExternals) {
		this.mainOntology = ontology;
		this.depdencyOntologies = Collections.unmodifiableCollection(necessaryExternals);
	}

	protected void loadOntology() throws OWLOntologyCreationException {
		logger.info("Converting input streams...");
		this.manager = OWLManager.createOWLOntologyManager();
		Iterator logOntoName = this.depdencyOntologies.iterator();

		while (logOntoName.hasNext()) {
			InputStream depdencyOntology = (InputStream) logOntoName.next();
			this.manager.loadOntologyFromOntologyDocument(depdencyOntology);
		}

		this.ontology = this.manager.loadOntologyFromOntologyDocument(this.mainOntology);
		this.loadedOntologyPath = "file upload";
		String logOntoName1;
		if (!this.ontology.isAnonymous()) {
			logOntoName1 = ((IRI) this.ontology.getOntologyID().getOntologyIRI().get()).toString();
		} else {
			logOntoName1 = "Anonymous";
			logger.info("Ontology IRI is anonymous. Use loaded URI/IRI instead.");
		}

		logger.info("Ontologies loaded! Main Ontology: " + logOntoName1);
	}
}